package com.dsteiner.gwt.cafeteriasophia.client.domain;

import java.util.Date;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.dsteiner.widgets.client.domain.ModelObject;
import com.dsteiner.widgets.client.domain.Searchable;
import com.dsteiner.widgets.client.domain.SearchableId;
import com.dsteiner.widgets.client.domain.SearchableProperty;
import com.dsteiner.widgets.client.domain.SearchableRefId;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Searchable
public class Stock extends ModelObject implements HasValue, HasProduct{

	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    @SearchableId
    private String id;
	
	@Persistent
    @SearchableRefId
    private String productName;
	
    @Persistent
    @SearchableProperty
	private double amount;
    
    @Persistent
    private Date registeredDate;
 
	public Stock() {
	}

	@Override
	public String getId() {
		return id;
	}
	@Override
	public void setId(String id) {
		this.id = id;
	}
	public double value(){
		Double cost = getProduct().getCost();
		double costAsDouble = 1;
		if(cost != null)
			costAsDouble = cost;
		return amount * costAsDouble; 
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}
	public double getAmount() {
		return amount;
	}
	@Override
	public Date getRegisteredDate() {
		return registeredDate;
	}
	public void setRegisteredDate(Date registeredDate) {
		this.registeredDate = registeredDate;
	}
	public Product getProduct() {
		return Product.getProduct(productName);
	}
	public void setProduct(Product product) {
		this.productName = product.getName();
	}
	@Override
	public String toString() {
		return "Inventario:"+productName+"|"+amount;
	}
}
